package com.rnd.kx.mvp.presenter;

import com.rnd.kx.mvp.base.BasePresenter;
import com.rnd.kx.mvp.bean.IndexBean;
import com.rnd.kx.mvp.bean.LoginBean;
import com.rnd.kx.mvp.bean.SearchBean;
import com.rnd.kx.mvp.contract.MainContract;
import com.rnd.kx.mvp.http.bean.BaseObjectBean;
import com.rnd.kx.mvp.http.net.RxScheduler;
import com.rnd.kx.mvp.model.MainModel;

import io.reactivex.functions.Consumer;

public class MainPresenter extends BasePresenter<MainContract.View> implements MainContract.Presenter {

    private final MainModel model;

    public MainPresenter() {
        model = new MainModel();
    }

    @Override
    public void login(String username, String password) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()){
            return;
        }
        mView.hideLoading();
        model.login(username,password)
                .compose(RxScheduler.<BaseObjectBean<LoginBean>>Flo_io_main())
                .as(mView.<BaseObjectBean<LoginBean>>bindAutoDispose())
                .subscribe(new Consumer<BaseObjectBean<LoginBean>>() {
                    @Override
                    public void accept(BaseObjectBean<LoginBean> bean) throws Exception {
                        mView.loginResult(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });


    }

    @Override
    public void getSearch(String keywords) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()){
            return;
        }
        mView.hideLoading();
        model.getSearch(keywords)
                .compose(RxScheduler.<BaseObjectBean<SearchBean>>Flo_io_main())
                .as(mView.<BaseObjectBean<SearchBean>>bindAutoDispose())
                .subscribe(new Consumer<BaseObjectBean<SearchBean>>() {
                    @Override
                    public void accept(BaseObjectBean<SearchBean> bean) throws Exception {
                        mView.searchResult(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void getIndex() {
        if (!isViewAttached()){
            return;
        }
        mView.hideLoading();
        model.getIndex()
                .compose(RxScheduler.<BaseObjectBean<IndexBean>>Flo_io_main())
                .as(mView.<BaseObjectBean<IndexBean>>bindAutoDispose())
                .subscribe(new Consumer<BaseObjectBean<IndexBean>>() {
                    @Override
                    public void accept(BaseObjectBean<IndexBean> bean) throws Exception {
                        mView.indexResult(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }
}
